package org.mozhu.mboot.core.file.util;

import com.google.common.base.Joiner;

import java.io.File;

public class PathUtils {

    public static final String SEPARATOR = "/";

    public static String join(String... paths) {
        return Joiner.on(SEPARATOR).join(paths);
    }

    public static String standardize(String path) {
        if (path == null) {
            return null;
        }
        path = path.replace("\\", SEPARATOR).replaceAll("/+", SEPARATOR);
        path = path.replace("https:/", "https://");
        path = path.replace("http:/", "http://");
        return path;
    }

    public static String standardizeJoin(String... paths) {
        return standardize(join(paths));
    }

    public static String getResourcePath(String resourceBasePath, String... paths) {
        return getResourcePath(resourceBasePath, join(paths));
    }

    public static String getResourcePath(String resourceBasePath, String path) {
        path = standardize(path);
        if (path.startsWith(SEPARATOR)) {
            return resourceBasePath + path;
        }
        return resourceBasePath + SEPARATOR + path;
    }

    public static String getRelativePath(File directory, File file) {
        String absoluteBasePath = directory.getAbsolutePath();
        String absolutePath = file.getAbsolutePath();
        if (absolutePath.startsWith(absoluteBasePath)) {
            return absolutePath.substring(absoluteBasePath.length());
        }
        return null;
    }
}
